﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Controllers.Filters;
namespace CMN.Controllers
{
#foreach($u in $TableInfos)
#beforeall
#each
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class ${u.TabName}Controller : Controller
    {
        
        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View("Index");//页面地址
        }

        /// <summary>
        /// 返回${u.TabCaption}列表
        /// </summary>
        /// <returns>json</returns>
        public string List()
        {
            //返回json数组
          
            int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1, DataType.Int);
            int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1, DataType.Int);
            QueryParam qp = new QueryParam();
            qp.PageIndex = pageIndex / pageSize + 1; ;
            qp.PageSize = pageSize;
            int recordNum = 0;
            IsoDateTimeConverter jsDataConvert= new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
            IDataService<${u.TabName}Entity> ${u.TabName}Sv = ServiceBuilder.Build${u.TabName}Service();
            List<${u.TabName}Entity> ${u.TabName.ToLower()}List = ${u.TabName}Sv.List(qp, out recordNum);
            returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(${u.TabName.ToLower()}List, Formatting.Indented, jsDataConvert), "}");
            return returnJson;
 
        }

        /// <summary>
        /// 查看${u.TabCaption} 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look(string actionId)
        {
           IDataService<${u.TabName}Entity> ${u.TabName}Sv = ServiceBuilder.Build${u.TabName}Service();
            int intId=Utils.StringToInt(actionId,0);
            ${u.TabName}Entity ${u.TabName.ToLower()} = ${u.TabName}Sv.Look(intId);
            if (${u.TabName.ToLower()} != null)
                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(${u.TabName.ToLower()}), "}");
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 更新${u.TabCaption}
        /// </summary>
        /// <param name="actionId"></param>
        /// <returns>json</returns>
        public string Update(string actionId)
        {
            int intId = Utils.StringToInt(actionId, 0);
            if (intId != 0)
            {
                ${u.TabName}Entity ${u.TabName.ToLower()} = new ${u.TabName}Entity();
                ${u.TabName.ToLower()}.DataTable_Action = DataTableAction.Update;
#foreach($c in $u.ColumnInfos)  
#beforeall
#each 
#if($c.IsPK!="True")
#if($c.TypeName=="bit")
                ${u.TabName.ToLower()}.${c.ColumnName} = (bool)Utils.GetQueryAndFormString("${c.ColumnName}", this, ${c.Length}, #if(${c.cisNull}!="True") 1 #else 0#end, DataType.Byte);
#else
                ${u.TabName.ToLower()}.${c.ColumnName} = (${c.TypeCSharpName})Utils.GetQueryAndFormString("${c.ColumnName}", this, ${c.Length}, #if(${c.cisNull}!="True") 1 #else 0#end, DataType.${c.TypePostName});
#end
#else
                ${u.TabName.ToLower()}.$c.ColumnName = intId;
#end
#end
                IDataService<${u.TabName}Entity> ${u.TabName}Sv = ServiceBuilder.Build${u.TabName}Service();
                if (${u.TabName}Sv.Update(${u.TabName.ToLower()})== "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
           
            return returnJson;
        }

        /// <summary>
        /// 新增${u.TabCaption}
        /// </summary>
        /// <returns>json</returns>
        public string Insert()
        {
            ${u.TabName}Entity ${u.TabName.ToLower()} = new ${u.TabName}Entity();
            ${u.TabName.ToLower()}.DataTable_Action = DataTableAction.Insert;
#foreach($c in $u.ColumnInfos)  
#beforeall
#each 
#if($c.IsPK!="True")
#if($c.TypeName=="bit")
            ${u.TabName.ToLower()}.${c.ColumnName} = (bool)Utils.GetQueryAndFormString("${c.ColumnName}", this, ${c.Length}, #if(${c.cisNull}!="True") 1 #else 0#end, DataType.Byte);
#else
            ${u.TabName.ToLower()}.${c.ColumnName} = (${c.TypeCSharpName})Utils.GetQueryAndFormString("${c.ColumnName}", this, ${c.Length}, #if(${c.cisNull}!="True") 1 #else 0#end, DataType.${c.TypePostName});
#end
#end
#end
           IDataService<${u.TabName}Entity> ${u.TabName}Sv = ServiceBuilder.Build${u.TabName}Service();
            if (${u.TabName}Sv.Insert(${u.TabName.ToLower()})== "")
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 删除${u.TabCaption}
        /// </summary>
        /// <param name="actionId"></param>
        /// <returns>json</returns>
        public string Delete(string actionId)
        {
            int intId = Utils.StringToInt(actionId, 0);
            if (intId != 0)
            {
                ${u.TabName}Entity ${u.TabName.ToLower()} = new ${u.TabName}Entity();
#foreach($c in $u.ColumnInfos)  
#beforeall
#each 
#if($c.IsPK=="True")
                ${u.TabName.ToLower()}.${c.ColumnName} = intId;
#end
#end
                IDataService<${u.TabName}Entity> ${u.TabName}Sv = ServiceBuilder.Build${u.TabName}Service();
                if (${u.TabName}Sv.Delete(${u.TabName.ToLower()})== "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
            return returnJson;
        }

        /// <summary>
        /// 批量删除${u.TabCaption}
        /// </summary>
        /// <param name="idList">id列表 234_4_5</param>
        /// <returns>json</returns>
        public string DeleteList(string actionId)
        {
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (regex.IsMatch(actionId))
            {

                IDataService<${u.TabName}Entity> ${u.TabName}Sv = ServiceBuilder.Build${u.TabName}Service();
                if (${u.TabName}Sv.DeleteList(actionId)== "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";

            }
            else
            {
                returnJson = "{success:'false',message:'参数类型不正确'}";
            }
            return returnJson;
        }

    }
}
#end